#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPushButton>
#include <QStringLiteral>
#include <QTextDocument>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);
    this->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    this->setWindowTitle(QStringLiteral("QSS学习工具"));
    this->setFixedSize(1000, 600);
    ui->pushButton->setText("class=btn id=pushButton name=test");
    ui->textEdit->clear();
    ui->textEdit->setStyleSheet("font-family:'Courier New','Source Code Pro', monospace; font-size:20px;");
    ui->textEdit->append("/* 1.通用选择器\* */");
    ui->textEdit->append("*{color:red;}");
    ui->textEdit->append("/* 2.类型选择器 */");
    ui->textEdit->append("QPushButton{background:blue;}");
    ui->textEdit->append("/* 3.ID选择器 */");
    ui->textEdit->append("#pushButton{font-family:'Courier New';}");
    ui->textEdit->append("/* 4.类选择器 */");
    ui->textEdit->append(".btn{color:white;}");
    ui->textEdit->append("/* 5.伪类选择器 */");
    ui->textEdit->append("QPushButton:hover{color: yellow; }");
    ui->textEdit->append("/* 6.属性选择器 */");
    ui->textEdit->append("QPushButton[name=test]{font-size:30px;}");
    ui->textEdit->append("/* 7.包含选择器 */");
    ui->textEdit->append("QMainWindow QPushButton{border-top:3px solid red;}");
    ui->textEdit->append("/* 8.子元素选择器 */");
    ui->textEdit->append("QMainWindow > QWidget > QPushButton{border-bottom:3px solid magenta;}");
    ui->textEdit->append("/* 9.Subcontrol选择器 */");
    //ui->textEdit->append("QCheckBox::indicator{border:1px solid green;}");
    ui->textEdit->append("QCheckBox::indicator{background:green;}");

    ui->treeWidget->setColumnCount(1);
    ui->treeWidget->setHeaderLabel("窗口控件结构树");
    QTreeWidgetItem *topLevelItem = new QTreeWidgetItem(ui->treeWidget, QStringList("QMainWindow"));
    QTreeWidgetItem *secondLevelItem = new QTreeWidgetItem(topLevelItem, QStringList("QWidget"));
    QTreeWidgetItem *thirdLevelItem1 = new QTreeWidgetItem(secondLevelItem, QStringList("QTextEdit"));
    QTreeWidgetItem *thirdLevelItem2 = new QTreeWidgetItem(secondLevelItem, QStringList("QPushButton"));
    QTreeWidgetItem *thirdLevelItem3 = new QTreeWidgetItem(secondLevelItem, QStringList("QRadioButton"));
    QTreeWidgetItem *thirdLevelItem4 = new QTreeWidgetItem(secondLevelItem, QStringList("QCheckBox"));
    QTreeWidgetItem *thirdLevelItem5 = new QTreeWidgetItem(secondLevelItem, QStringList("QLabel"));
    QTreeWidgetItem *thirdLevelItem6 = new QTreeWidgetItem(secondLevelItem, QStringList("QLineEdit"));
    QTreeWidgetItem *thirdLevelItem7 = new QTreeWidgetItem(secondLevelItem, QStringList("QProgressBar"));
    QTreeWidgetItem *thirdLevelItem8 = new QTreeWidgetItem(secondLevelItem, QStringList("QTimeEdit"));
    QTreeWidgetItem *thirdLevelItem9 = new QTreeWidgetItem(secondLevelItem, QStringList("QDateEdit"));
    QTreeWidgetItem *thirdLevelItem10 = new QTreeWidgetItem(secondLevelItem, QStringList("QDateTimeEdit"));
    QTreeWidgetItem *thirdLevelItem11 = new QTreeWidgetItem(secondLevelItem, QStringList("QLCDNumber"));
    QTreeWidgetItem *thirdLevelItem12 = new QTreeWidgetItem(secondLevelItem, QStringList("QTreeWidget"));
    ui->treeWidget->addTopLevelItem(topLevelItem);
    topLevelItem->setExpanded(true);
    secondLevelItem->setExpanded(true);
    ui->pushButton->setToolTip(QStringLiteral("点击按钮查看样式效果"));
    ui->progressBar->setRange(0, 0);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(setStyle()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setStyle()
{
    QString style = ui->textEdit->document()->toPlainText().trimmed();
    if(style == ""){
        return;
    }
    //QMessageBox::information(this, QStringLiteral("样式"), style);
    ui->pushButton->setProperty("class", "btn");
    ui->pushButton->setProperty("name", "test");
    this->setStyleSheet(style);

    ui->lcdNumber->display(ui->lcdNumber->intValue() + 1);

}

